<?php
/**
 * Created by PhpStorm.
 * User: iamya
 * Date: 2017/11/27
 * Time: 9:48
 */

namespace app\home\controller;


use app\common\BusinessModel;
use app\common\BusinessRecordModel;
use think\Exception;
use think\Request;
use think\Validate;

class BusinessController extends BaseController
{
    private $businessModel;
    private $businessRecordModel;
    private $userInfo;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->businessModel = new BusinessModel();
        $this->businessRecordModel = new BusinessRecordModel();
        $this->userInfo = $this->getDduser();
    }

    /**
     * 商机列表
     */
    public function index()
    {
        $data = $this->request->param();
        $map = ['contacts_id' => $this->userInfo['userid']];
        if (!empty($data['customer_id'])) {
            $map['customer_id'] = $data['customer_id'];
        }
        if (!empty($data['create_userid'])) {
            $map['create_userid'] = $data['create_userid'];
        }
        if (!empty($data['status'])) {
            $map['status'] = $data['status'];
        }
        if (!empty($data['is_deleted'])) {
            $map['is_deleted'] = $data['is_deleted'];
        }
        $startTime = $this->request->param('start_time', '1970-01-01 08:00:01');
        $endTime = $this->request->param('end_time', '5138-11-16 17:46:39');
        $map['create_time'] = ['between', [strtotime($startTime), strtotime($endTime)]];
        $list = $this->businessModel->where($map)->paginate(12, false, ['query' => $data]);
        return $this->callBack(0, '商机列表', $list);
    }

    /**
     * 商机转化
     */
    public function add($data = [])
    {
        if (empty($data)) {
            $data = $this->request->param();
        }
        $vadata = [
            'is_deleted' => 0,
            'delete_userid' => '',
            'delete_time' => 0,
            'contacts_id' => $this->userInfo['userid'],
            'create_userid' => $this->userInfo['userid']
        ];
        $data = array_merge($vadata, $data);
        $validate = new Validate([
            ['name', 'require', '请输入商机名称'],
            ['customer_id', 'require', '请选择客户'],
            ['create_userid', 'require', '请先登录'],
            ['status', 'require', '请选择商机状态'],
            ['contacts_id', 'require', '请选择联系人']
        ]);
        $result = $validate->check($data);
        if (true !== $result) {
            return $this->callBack(1, $validate->getError());
        }
        if ($this->businessModel->data($data)->save()) {
            return $this->callBack(0, '商机添加成功', $this->businessModel->getLastInsID());
        } else {
            return $this->callBack(2, '商机添加失败');
        }
    }

    /**
     * 商机修改
     */
    public function edit()
    {
        $data = $this->request->param();

        $validate = new Validate([
            ['business_id', 'require', '请选择修改的商机'],
            ['name', 'require', '请输入商机名称'],
            ['customer_id', 'require', '请选择客户'],
            ['create_userid', 'require', '请先登录'],
            ['status', 'require', '请选择商机状态'],
            ['contacts_id', 'require', '请选择联系人']
        ]);
        $result = $validate->check($data);
        if (true !== $result) {
            return $this->callBack(1, $validate->getError());
        }
        if ($this->businessModel->data($data)->save()) {
            return $this->callBack(0, '商机修改成功', $data['contacts_id']);
        } else {
            return $this->callBack(2, '商机修改失败');
        }
    }

    /**
     * 商机删除
     * @return \think\response\Json
     */
    public function del()
    {
        $businessId = $this->request->param('business_id', 0);
        try {
            $this->businessModel
                ->where(['business_id' => $businessId, 'contacts_id' => $this->userInfo['userid']])
                ->save(['is_deleted' => 1, 'delete_userid' => $this->userInfo['userid'], 'delete_time' => time()]);
        } catch (Exception $e) {
            return $this->callBack(2, $e->getMessage());
        }
        return $this->callBack(0, '商机删除成功');
    }

    /**
     * 单条商机信息
     * @return \think\response\Json
     */
    public function getBusiness()
    {
        $businessId = $this->request->param('business_id', 0);
        $info = $this->businessModel->where('business_id', $businessId)->find();
        return $this->callBack(0, '', $info);
    }

    /**
     * 获取商机流水
     * @return \think\response\Json
     */
    public function getBusinessRecord()
    {
        $businessId = $this->request->param('business_id', 0);
        $data = $this->request->param();
        $map = ['business_id' => $businessId];
        if (!empty($data['word'])) {
            $words = explode(' ', $data['word']);
            foreach ($words as $word) {
                $map['name|description'][] = ['like', '%' . $word . '%'];
            }
            $map['name|description'][] = 'or';
        }
        $startTime = $this->request->param('start_time', '1970-01-01 08:00:01');
        $endTime = $this->request->param('end_time', '5138-11-16 17:46:39');
        $map['create_time'] = ['between', [strtotime($startTime), strtotime($endTime)]];
        $list = $this->businessModel->where($map)->order('sort,create_time desc')->paginate(12, false, ['query' => $data]);
        return $this->callBack(0, '', $list);
    }

    /**
     * 添加商机流水
     * @return \think\response\Json
     */
    public function addBusinessRecord()
    {
        $data = $this->request->param();
        $data['create_time']=time();
        $validate = new Validate([
            ['business_id', 'require', '请选择商机'],
//            ['name','require','请填写流水标题'],
            ['description', 'require', '请填写流水内容'],
        ]);
        $result=$validate->check($data);
        if (true!==$result){
            return $this->callBack(1,$validate->getError());
        }
        if ($this->businessRecordModel->data($data)->save()){
            return $this->callBack(0,'流水添加成功',$this->businessRecordModel->getLastInsID());
        }else{
            return $this->callBack(2,$this->businessRecordModel->getError());
        }
    }

}